package com.example.inventory.model.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 商品SKU实体类
 * 对应数据库表 t_product_sku
 * 
 * @author mall-team
 */
@Entity
@Table(name = "t_product_sku")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ProductSku {

    /**
     * SKU ID（主键）
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * SKU 编码（业务主键）
     */
    @Column(name = "sku_code", nullable = false, unique = true, length = 64)
    private String skuCode;

    /**
     * SKU 名称
     */
    @Column(name = "sku_name", nullable = false, length = 255)
    private String skuName;

    /**
     * 商品ID
     */
    @Column(name = "product_id", nullable = false)
    private Long productId;

    /**
     * 价格
     */
    @Column(name = "price", nullable = false, precision = 10, scale = 2)
    private BigDecimal price;

    /**
     * 库存数量
     */
    @Column(name = "stock_quantity", nullable = false)
    private Integer stockQuantity;

    /**
     * 已预留库存数量
     */
    @Column(name = "reserved_quantity", nullable = false)
    @Builder.Default
    private Integer reservedQuantity = 0;

    /**
     * 可用库存数量（库存数量 - 已预留数量）
     */
    @Transient
    public Integer getAvailableQuantity() {
        return stockQuantity - reservedQuantity;
    }

    /**
     * 状态（ACTIVE-上架，INACTIVE-下架）
     */
    @Column(name = "status", nullable = false, length = 32)
    @Builder.Default
    private String status = "ACTIVE";

    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
}

